package com.wt.controller;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wt.pojo.Distributor;
import com.wt.pojo.WithdrawOrder;
import com.wt.service.DistributorService;
import com.wt.service.WithDrawService;
import com.wt.service.WithdrawOrderService;
import com.wt.util.Pager;
import com.wt.util.Pagination;
import com.wt.util.StringUtil;


@Controller
@RequestMapping("/withdraw")
public class WithdrawOrderController {
	
	@Resource
	private WithdrawOrderService withdrawOrderService;
	@Resource
    private WithDrawService withDrawService;
	@Resource
    private DistributorService distributorService;
	
	
	
	@RequestMapping(value = "")
    public String index(Model model) throws Exception{
        return "auditing/withdrawList";
    }
	
	/**
	 * 审核列表数据
	 * @param model
	 * @param page
	 * @param pageSize
	 * @param beginDate
	 * @param endDate
	 * @param status 状态
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "list", method = RequestMethod.POST)
	public String list(Model model, Integer page, Integer pageSize, String beginDate, String endDate,
			Integer status, HttpServletRequest request) {
		Pager pager = new Pager();
		pager.setPageth(page == null ? 1 : page);
		pager.setPageSize(pageSize == null ? Pager.DEFAULT_DISPLAY_PAGETHS_COUNT : pageSize);
		
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date start = null;
        Date end = null;
        Long timeStart = null;
        Long timeEnd = null;
        if (!StringUtil.isEmpty(beginDate) && !StringUtil.isEmpty(endDate)) {
            try {
                start = format.parse(beginDate);
                end = format.parse(endDate);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            timeStart = start.getTime();
            timeEnd = end.getTime() + (24*60*60*1000-1);
        }
        
		
		Pagination<WithdrawOrder> withdrawPagination = withdrawOrderService.findList(timeStart, timeEnd,pager);
		model.addAttribute("withdrawPagination", withdrawPagination);
		return "auditing/withdrawList_frag";
	}
	
	@RequestMapping(value = "edit", method = RequestMethod.POST)
	@ResponseBody
	public WithdrawOrder edit( Integer id) {
		WithdrawOrder withdrawOrder = withdrawOrderService.getWithdrawOrder(id);
		return withdrawOrder;
	}
	
	@RequestMapping(value = "updateWithdraw", method = RequestMethod.POST)
	@ResponseBody
	public int updateWithdraw( Integer id,Integer status ,String remark) {
		WithdrawOrder withdrawOrder = withdrawOrderService.getWithdrawOrder(id);
		//驳回则只修改状态
		if(status == 2){
			return withdrawOrderService.updateWithdraw(id, status, remark);
		}else{
			//成交总金额 
			BigDecimal accountTotal = withDrawService.selectAccountTotal(withdrawOrder.getDistributor().getId());
			 if(accountTotal != null ){
					//可提现金额
			        BigDecimal commissionTotal = accountTotal.multiply(withdrawOrder.getDistributor().getProportion());
			        //-1表示小于,0是等于,1是大于
			        int result =  commissionTotal.compareTo(withdrawOrder.getMoney());
			        //可提现金额小于申请提现金额		
			        if(result == -1){
			        	//status == 通过
			        	if(status == 3){
			        		return -1;
			        	}else{
			        		return withdrawOrderService.updateWithdraw(id, status, remark);
			        	}
			        	
			        }else{
			        	
			        	Distributor Distributor = withdrawOrder.getDistributor();
						BigDecimal money = null ;
						if(Distributor.getCommissionWithdraw() == null){
							if(withdrawOrder.getMoney() != null){
								money = withdrawOrder.getMoney();
							}
						}else{
							if(withdrawOrder.getMoney() != null){
								money = Distributor.getCommissionWithdraw().add(withdrawOrder.getMoney());
							}
						}
						//更改Distributor已提现金额
				    	 distributorService.updateCommissionWithdraw(Distributor.getId(),money);
				    	return withdrawOrderService.updateWithdraw(id, status, remark);
		        }
	        }else{
	        	return -1;
	        }
		}
		
    	 
	}
}
